﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using public_dbOpt;

namespace prjEMR22_154
{
    public partial class PatientInfo_inquire : Form
    {
        dbOPT dataOpt = new dbOPT();
        public static string strHSPID;//跨窗体公共变量--住院号

        public PatientInfo_inquire()
        {
            InitializeComponent();
        }

        private void PatientInfo_inquire_Load(object sender, EventArgs e)
        {
            //绑定科室下拉列表
            dataOpt.BindDropdownlist("departinfo", DepartName, 1);

            //将初始出生日期、就诊日期设置为一个空格" "以表示空值
            this.Birthday.Format = DateTimePickerFormat.Custom; //出生日期
            this.Birthday.CustomFormat = " ";
            this.HspTime.Format = DateTimePickerFormat.Custom; //就诊日期
            this.HspTime.CustomFormat = " ";
        }

        private void btnFind_Click(object sender, EventArgs e) //查询按键
        {
            string StrFind = "";    //执行语句
            DataSet ds = new DataSet();//创建数据集
            
            if (HspID.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and HspID like '%{0}%' ", HspID.Text);
                else
                    StrFind = StrFind + String.Format("HspID like '%{0}%' ", HspID.Text);
            }
            if (BedID.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and BedID like '%{0}%' ", BedID.Text);
                else
                    StrFind = StrFind + String.Format("BedID like '%{0}%' ", BedID.Text);
            }
            if (Pa_Name.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and Name like '%{0}%' ", Pa_Name.Text);
                else
                    StrFind = StrFind + String.Format("Name like '%{0}%' ", Pa_Name.Text);
            }
            if (Gender.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and Gender like '%{0}%' ", Gender.Text);
                else
                    StrFind = StrFind + String.Format("Gender like '%{0}%' ", Gender.Text);
            }
            if (DepartName.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and DepartName like '%{0}%' ", DepartName.Text);
                else
                    StrFind = StrFind + String.Format("DepartName like '%{0}%' ", DepartName.Text);
            }
            if (MedInsuranceType.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and MedInsuranceType like '%{0}%' ", MedInsuranceType.Text);
                else
                    StrFind = StrFind + String.Format("MedInsuranceType like '%{0}%' ", MedInsuranceType.Text);
            }
            if (DoctorName.Text.Length > 0)
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and DoctorName like '%{0}%' ", DoctorName.Text);
                else
                    StrFind = StrFind + String.Format("DoctorName like '%{0}%' ", DoctorName.Text);
            }
            if (Birthday.Text != " ")
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and Birthday like '%{0}%' ", Birthday.Text);
                else
                    StrFind = StrFind + String.Format("Birthday like '%{0}%' ", Birthday.Text);
            }
            if (HspTime.Text != " ")
            {
                if (StrFind != "")
                    StrFind = StrFind + String.Format("and HspTime like '%{0}%' ", HspTime.Text);
                else
                    StrFind = StrFind + String.Format("HspTime like '%{0}%' ", HspTime.Text);
            }

            if (HspID.Text.Length > 0 || BedID.Text.Length > 0 || Pa_Name.Text.Length > 0 || Gender.Text.Length > 0 || DepartName.Text.Length > 0
                || MedInsuranceType.Text.Length > 0 || DoctorName.Text.Length > 0 || Birthday.Text != " " || HspTime.Text != " ")  //输入为非空查询
            {
                StrFind = "select * from patientinfo where " + StrFind + ";";   
                ds = dataOpt.BindDataGridView(dgvPatient, StrFind);     //执行查询语句
                if (ds != null && ds.Tables[0].Rows.Count != 0) {}//检查查询结果是否为空，非空则不执行任何操作
                else
                {
                    // 处理没有数据或查询失败的情况  
                    MessageBox.Show("查询为空。");
                }
            }
            else   //空查询，显示表中全部信息
            {
                StrFind = "select * from patientinfo;";
                dataOpt.BindDataGridView(dgvPatient, StrFind);
            }
            //dataOpt.BindDataGridView(dgvPatient,StrFind);
            //DateTime dateTimePicker = dateTimePicker1.Value;
            //dataOpt.BindDataGridView(dgvPatient, "select ");
            //MessageBox.Show(StrFind);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Birthday.Format = DateTimePickerFormat.Custom; //出生日期
            this.Birthday.CustomFormat = " ";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.HspTime.Format = DateTimePickerFormat.Custom; //就诊日期
            this.HspTime.CustomFormat = " ";
        }

        private void dateTimePicker2_ValueChanged_1(object sender, EventArgs e)
        {
            //就诊日期，这里可以个更改自己的需要的格式，例：yyyy年MM月dd日
            HspTime.CustomFormat = "yyyy-MM-dd";
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            //出生日期，这里可以个更改自己的需要的格式，例：yyyy年MM月dd日
            Birthday.CustomFormat = "yyyy-MM-dd";
        }

        private void dgvPatient_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0) // 确保单击的是数据行，而不是列标题  
            {  
                // 确保点击的是有效的单元格  
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0)  
                {  
                    // 获取点击的单元格所在的行对象  
                    DataGridViewRow row = dgvPatient.Rows[e.RowIndex];  
          
                    // 访问同一行的其他单元格的值  
                    // 假设你有一个名为 "HspID" 的列，并且你想获取这个列的值  
                    strHSPID = row.Cells["HspID"].Value.ToString();

                    PatientInfo_inquire_checked a = new PatientInfo_inquire_checked();  //展开病人数据
                    a.Show();
                }
            } 
        }

        private void DepartName_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



    }
}
